<?php
/*
 * @Author: your name
 * @Date: 2020-10-23 15:19:19
 * @LastEditTime: 2020-12-16 10:27:50
 * @LastEditors: Please set LastEditors
 * @Description: 开启该模块主机列表
 * @FilePath: /special/application/adminapiv3/service/module_manage/netfilter/NetfilterHostService.php
 */

namespace app\adminapiv3\service\module_manage\netfilter;

use app\adminapiv3\service\CommonHostService;
use app\common\definition\HostDefinition;
use app\common\service\module_manage\netfilter\HostLearnConfig;
use think\Db;


class NetfilterHostService extends CommonHostService
{
    public $module_name = 'netfilter';
    
    /**
     * @name:设置
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2020-01-06 13:05:54
     */
    public function config ($hostId_a)
    {
        $HostLearnConfig = new HostLearnConfig();
        $config = $HostLearnConfig->selectByHostId($hostId_a,'*');

        $firewall_config_a = $this->firewallConfig($hostId_a);
        $return = [];
        foreach ($config as $v)
        {
            $return[$v['host_id']] = [
                //是否已接管防火墙
                'controlled_firewall' => $v['controlled_firewall'],
                //防火墙默认行为参数
                'firewall_config' => isset($firewall_config_a[$v['host_id']]) ? $firewall_config_a[$v['host_id']] : [],
                //防火墙访问日志记录开启状态
                'log_status' => $v['log_status'],
                //学习状态
                'learn_status' => $v['learn_status'],
                //当前规则组状态
                'current_rule_group_status' => $v['current_rule_group_status'],
                //提醒虚拟规则是否有新的命中
                'virtual_rule_hit_remind' => $v['virtual_rule_hit_remind'],
                //学习配置
                'study_config' => [
                    //学习开始时间
                    'stime' => $v['stime'],
                    //学习结束时间
                    'etime' => $v['etime'],
                    //学习持续时间
                    'keep_time' => $v['keep_time'],
                    //是否自动
                    'auto' => $v['auto'],
                    //学习模式
                    'model' => $v['model'],
                    //默认处理方式
                    'allow' => $v['allow'],
                    //默认处理方式
                    'have_mac_addr' => $v['have_mac_addr'],
                    //是否立即下发
                    'now' => $v['now'],
                ],
            ];
        }
        
        return $return;
    }

    private function firewallConfig ($host_id_a)
    {
        $return = [];
        
        $linux_config = Db::name('netfilter_firewall_config_linux')->whereIn('host_id', $host_id_a)->select();
        foreach ($linux_config as $config)
        {
            unset($config['id']);
            $return[$config['host_id']] = $config;
            
        }
        $windows_config = Db::name('netfilter_firewall_config_windows')->whereIn('host_id', $host_id_a)->select();
        foreach ($windows_config as $config)
        {
            unset($config['id']);
            $return[$config['host_id']] = $config;
        }

        return $return;
    }

    /**
     * @name: 处理列表数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-23 16:18:05
     */
    public function processList ($data)
    {
        if (empty($data))
        {
            return [];
        }
        
        $return = [];
        $offline = [];

        foreach ($data as  $k => $v)
        {
            $hostId_a[] = $v['id'];
            $v['online'] == 0 ? $offline[] = $v['id'] : '' ;
        }

        $extraData = $this->extraData();

        $moduleData = $this->config($hostId_a);

        foreach ($data as $k => $v)
        {
            $return[$k] = $this->processOne($extraData, $v);
            !isset($moduleData[$v['id']]) ? : $return[$k][$this->module_name] = $moduleData[$v['id']];
        }

        return $return;
    }

    /**
     * @name 获取学习关系数据
     * @author wx
     * @date 2020/11/9 14:33
     */
    public function studyRelation()
    {
        $userInfo = $this->getLoginUserInfo();
        $groupId = isset($userInfo['rbac']['roles'][0]['group_id']) ? $userInfo['rbac']['roles'][0]['group_id'] : 0;
        $hostIdArr = Db::name('host')->where('group_id', $groupId)->column('id');


        $data = Db::name('host_learn_relation')
            ->whereIn('host_id_small', $hostIdArr)
            ->whereIn('host_id_big', $hostIdArr)
            ->select();

        return $data;
    }
}
